/*
 * Ticket.java
 *
 * Created on October 4, 2007, 4:19 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package drb;

import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author Asger
 */
public class Ticket {
    
    public int id;
    public int ownerCpr;
    public int price;
    public boolean firstClass;
    public Passenger passenger = null;
    public TrainRoute route = null;
    
    /** Creates a new instance of Ticket */
    public Ticket() {
    }
    
    public void fetchTrainRoute(Connection connection) throws SQLException {
        // Get the route
        String sql = "SELECT * FROM ticketdue NATURAL JOIN connections WHERE ticket_id="+id +" " +
                "ORDER BY arrival ASC";
        ResultSet result = connection.createStatement().executeQuery(sql);
        
        route = new TrainRoute();
        
        while (result.next()) {
            TrainConnection con = TrainConnection.parse(result);
            route.connections.add(con);
        }
    }
    
    public void fetchOwner(Connection connection) throws SQLException {
        // Get the owners information
        String sql = "SELECT * FROM passengers WHERE passenger_cpr="+ownerCpr;
        ResultSet result = connection.createStatement().executeQuery(sql);
        if (result.next()) {
            passenger = Passenger.parse(result);
        }
    }
    
    public static Ticket parse(ResultSet result) throws SQLException {
        Ticket ticket = new Ticket();
        
        ticket.id = result.getInt("ticket_id");
        ticket.ownerCpr = result.getInt("passenger_cpr");
        ticket.price = result.getInt("ticket_price");
        ticket.firstClass = (result.getString("class").equals("A"));
        
        return ticket;
    }
    
    public void printDetails() {
        assert(route != null && passenger != null);
        
        System.out.println("Ticket id:      " + id);
        System.out.println("Owner:          " + passenger);
        System.out.println("Connection:     " + route);
        System.out.println("Departure:      " + Command.formattedDateTime(route.getDeparture()));
        System.out.println("Arrival:        " + Command.formattedDateTime(route.getArrival()));
        System.out.println("Class:          " + (firstClass? "First class":"Economic class"));
        System.out.println("Price:          " + price + " kr");
    }
    
    public ArrayList<Reservation> fetchReservations(Connection con) throws SQLException {
        if (route == null)
            fetchTrainRoute(con);
        
        ArrayList<TrainRoute> segments = route.getRouteSegments();
        
        // Select all seats reserved for this ticket
        String sql = "SELECT DISTINCT s.*, r.train_number FROM seats s NATURAL JOIN reservations r WHERE " +
                "r.ticket_id="+id+" ORDER BY r.departure ASC";
        ResultSet result = con.createStatement().executeQuery(sql);
        
        // Create the list of reservations
        ArrayList<Reservation> reservations = new ArrayList<Reservation>();
        while (result.next()) {            
            Reservation reservation = new Reservation();
            reservation.seat = Seat.parse(result, "s.");
            reservation.ticket = this;
            
            // Find the correct train number
            int trainNumber = result.getInt("r.train_number");
            for (TrainRoute r : segments) {
                if (r.connections.get(0).trainNumber == trainNumber) {
                    reservation.route = r;
                    break;
                }
            } 
            
            reservation.seat.fetchDetails(con);
            reservations.add(reservation);
        }
        
        return reservations;
    }
    
    void printTransfers() {
        System.out.println("Transfers:      " + route.getTransfersString());
    }
    
    void printReservations(Connection con) throws SQLException {
        ArrayList<Reservation> reservations = fetchReservations(con);
        if (reservations.size() == 0)
        {
            System.out.println("Bookings:       None");
        }
        
        for (Reservation r : reservations) {
            System.out.println("Bookings:       " + r.seat + " on " + r.route);
        }
    }
}









